//
//  UILable+Extension.swift
//  SwiftLibrary
//
//  Created by yuange on 16/2/19.
//  Copyright © 2016年 taurusxi. All rights reserved.
//

import UIKit

private let kUILabelDefaultTextColor: UIColor = UIColor.black

extension UILabel
{
  // 高度固定，自适应宽度
  func lableWidth(_ text: NSString, maxHeight: CGFloat) -> CGRect {
    var lableRect = CGRect()
    let size = CGSize(width: 0, height: maxHeight)
    lableRect = text.boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: self.font], context: nil)
    return lableRect
  }

  // 高度固定，自适应宽度
  func lableWidth(_ text: NSString, fontSize: CGFloat, maxHeight: CGFloat) -> CGRect {
    var lableRect = CGRect()
    let size = CGSize(width: 0, height: maxHeight)
    lableRect = text.boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: fontSize)], context: nil)
    return lableRect
  }

  // 宽度固定，自适应高度
  func lableHeight(_ text: NSString, maxWidth: CGFloat) -> CGRect {
    var lableRect = CGRect()
    let size = CGSize(width: maxWidth, height: 0)
    lableRect = text.boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: self.font], context: nil)

    return lableRect
  }

  func labelHeightWithAttribute(_ attText: NSMutableAttributedString, maxWidth: CGFloat) -> CGRect {
    let size = CGSize(width: maxWidth, height: 0)
    return attText.boundingRect(with: size, options: [NSStringDrawingOptions.usesLineFragmentOrigin,NSStringDrawingOptions.usesFontLeading], context: nil)
  }

  func setAttributedStringWithAttributeString(_ attributeString: NSString, unitStr: NSString, attributeFont: UIFont, unitFont: UIFont, textColor: UIColor) {

    let str: NSString = "\(attributeString)\(unitStr)" as NSString
    let stringRange = NSMakeRange(0, attributeString.length)
    let unitStrRange = NSMakeRange(attributeString.length, unitStr.length)

    let attributeStr = NSMutableAttributedString(string: str as String)
    attributeStr.tx_setFont(attributeFont, range: stringRange)
    attributeStr.tx_setFont(unitFont, range: unitStrRange)
    attributeStr.tx_setTextColor(textColor)

    self.attributedText = attributeStr
  }

  func setAttributedString(_ orignFormatStr: NSString, formatArray: [String]?, orignColor: UIColor, attributteColor: UIColor) {

    if let formatList = formatArray {

      var orignStr = orignFormatStr

      var formatRangeArray: [NSRange] = []

      for formatStr in formatList {
        let range = orignStr.range(of: "%s")
        if range.location != NSNotFound {
          orignStr = orignStr.replacingCharacters(in: range, with: formatStr) as NSString
          formatRangeArray.append(orignStr.range(of: formatStr))
        }
      }
      let attributeStr = NSMutableAttributedString(string: orignStr as String)
      attributeStr.tx_setTextColor(orignColor)
      for range in formatRangeArray {
        attributeStr.tx_setTextColor(attributteColor, range: range)
      }
      self.attributedText = attributeStr
    } else {

      let attributeStr = NSMutableAttributedString(string: orignFormatStr as String)
      attributeStr.tx_setTextColor(orignColor)
      self.attributedText = attributeStr
    }
  }

  func setAttributedStringWithAttStr(_ attributeString: NSString, unitStr: NSString, attributeFont: UIFont, unitFont: UIFont, attrColor: UIColor, unitColor: UIColor) {

    let str: NSString = "\(attributeString)\(unitStr)" as NSString
    let stringRange = NSMakeRange(0, attributeString.length)
    let unitStrRange = NSMakeRange(attributeString.length, unitStr.length)

    let attributeStr = NSMutableAttributedString(string: str as String)
    attributeStr.tx_setFont(attributeFont, range: stringRange)
    attributeStr.tx_setTextColor(attrColor, range: stringRange)
    attributeStr.tx_setFont(unitFont, range: unitStrRange)
    attributeStr.tx_setTextColor(unitColor, range: unitStrRange)

    self.attributedText = attributeStr
  }

  convenience init(textColor: UIColor, fontSize: CGFloat, maxWidth: CGFloat = 0) {
    self.init()
    self.textColor = textColor
    self.font = UIFont.systemFont(ofSize: fontSize)

    if maxWidth > 0 {
      numberOfLines = 0
      // 设定布局的最大宽度,好让系统知道在哪个地方换行
      preferredMaxLayoutWidth = maxWidth
    }
  }
}
